import Vue from 'vue'
// 引入vuex-store
import store from './store/Store'
import App from './App.vue'
import './plugins/element.js'
import router from './router'
import './assets/css/global.css'
import './assets/fonts/iconfont.css'
import TreeTable from 'vue-table-with-tree-grid'
import axios from 'axios'
import './constants/index'
// 导入富文本编辑器对应的样式
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme
import * as echarts from 'echarts'

// 当如nprogress 包对应的js和css 用于加载进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.prototype.$Ip = 'http://8.137.61.44:8888/'
Vue.prototype.$serverIp = Vue.prototype.$Ip + 'api/private/v1/'

// 配置请求的跟路径
axios.defaults.baseURL = Vue.prototype.$serverIp
// 给请求头添加Authorization
axios.interceptors.request.use((config) => {
  // 展示进度条
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})

// 在response拦截器中，隐藏进度条
axios.interceptors.response.use((config) => {
  NProgress.done()
  return config
})

// 富文本编辑器全局注册
Vue.use(VueQuillEditor /* { default global options } */)
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.filter('dateFormat', function (originVal) {
  const dt = new Date(originVal)

  // 年
  const y = dt.getFullYear()
  // 月 getMonth返回的是从0 开始的，所以需要加1；如果不是两位，在前面补0
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  // 日
  const d = (dt.getDate() + '').padStart(2, '0')
  // 时
  const hh = (dt.getHours() + '').padStart(2, '0')
  // 分
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  // 时
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  // 'yyyy-mm-dd hh:mm:ss'
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
Vue.component('tree-table', TreeTable)

// echarts 图标显示与防抖自定义函数
Vue.directive('auto-resize', {
  // 当绑定元素被插入到 DOM 中时
  inserted: function (el, binding) {
    const chart = echarts.init(el)
    el.__chart__ = chart

    // if (binding.value) {
    //   el.__chart__.setOption(binding.value)
    //   el.__chart__.on('click', (a) => {
    //     console.log(a.name)
    //   })
    // }
    const debouncedResize = _.throttle(() => {
      el.__chart__.resize()
    }, 200)
    window.addEventListener('resize', debouncedResize)
    el.__resize__ = debouncedResize

    console.log([el], 'ee')
  },
  unbind(el) {
    window.removeEventListener('resize', el.__resize__)
    if (el.__chart__) {
      el.__chart__.dispose()
      el.__chart__ = null
    }
  },
})
Vue.directive('permission', {
  inserted: function (el, binding, vnode) {
    const user = []
    const users = []
    const roleList = binding.value
    const rid = vnode.context.$store.state.userData.rid
    const rolesLis = vnode.context.$store.state.rolesList

    rolesLis.forEach((role) => {
      if (role.id === rid || rid === 0) {
        return user.push(role)
      }
    })
    function getuserRolesId(user) {
      // console.log(users)
      user.forEach((user) => {
        // console.log(user)
        if (!user.children) {
          return users.push(user.id)
        } else {
          getuserRolesId(user.children)
        }
      })
    }
    getuserRolesId(user)
    const hasPermission = users.some((role) => roleList.includes(role))
    if (!hasPermission) {
      el.style.display = 'none'
    }
  },
})
new Vue({
  router,
  store,
  created() {
    console.log(this.$store.state)
  },
  render: (h) => h(App),
}).$mount('#app')
